package com.gxa.shop.servlet.order; /**
 * ${PACKAGE_NAME}
 * User: coma
 * Description:
 * Version: V1.0
 */

import com.alibaba.fastjson.JSON;
import com.gxa.shop.bean.vo.CheckInfoVo;
import com.gxa.shop.commons.r.R;

import com.gxa.shop.service.order.OrderService;
import com.gxa.shop.service.order.impl.OrderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "CheckoutServlet", value = "/member/order/pre")
public class AfterCartListOrderServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=UTF-8");
        OrderService orderService = new OrderServiceImpl();

        String userIdParam = request.getParameter("userId");
        String addressIdParam = request.getParameter("addressId");

        if (userIdParam == null || addressIdParam == null) {
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            PrintWriter writer = response.getWriter();
            writer.write(JSON.toJSONString(R.error("缺少必要的参数")));
            writer.flush();
            writer.close();
            return;
        }

        int userId = Integer.parseInt(userIdParam);
        int addressId = Integer.parseInt(addressIdParam);

        CheckInfoVo checkInfoVo = orderService.getCheckInfo(userId, addressId);
        R<CheckInfoVo> result = R.ok(checkInfoVo);
        PrintWriter writer = response.getWriter();
        writer.write(JSON.toJSONString(result));
        writer.flush();
        writer.close();
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
